<?php
session_start();

include_once dirname(__FILE__).'/../../db/rdb/factory/RDBDAOFactory.php';

if(isset($_POST['userid']) && isset($_POST['username']) 
		&& isset($_POST['password']) && isset($_POST['email'])) {
	// remember md5(password)
	$rdbFactory = DAOFactory::getDAOFactory(DAOFactory::$RDB);
	$userDAO = $rdbFactory->getUserDAO();
	$maxUserPK = $userDAO->getMaxPK();
	$user = new User($maxUserPK+1, $_POST['userid'], $_POST['username'], $_POST['password'], $_POST['email']);
	$r = $userDAO->insert($user);
	
	$registedUser = $userDAO->authenticate($user->getUserID(), $user->getPassword()); 
	if($r > 0) {
		echo json_encode(
				array(
						'success' => true,
						'user' => array(
								'username' => $user->getUserName(),
								'userid' => $user->getUserID(),
								'email' => $user->getEmail()
						)
				)
			);
		$_SESSION['user'] = serialize($registedUser);
	} else {
		echo json_encode(array('success' => false));
	}

}

?>